<?php

function MakeSign($appsecret, &$params, $content, $rejectEmptyCharacters)
{
    $str = $appsecret;
    ksort($params);
    foreach ($params as $key => $value) 
    {
        if ('sign' == $key) {
            continue;
        }

        if (!empty($key) && !empty($value)) {
            $str .= $key . $value;
        }
    }
    $str .= $content;
    $str .= $appsecret;
    if ($rejectEmptyCharacters) 
    {
        // 去除空白字符
        $str = str_replace(array("\t", "\r", "\n", " "), "", $str);
    }
    return strtoupper(md5($str));
}

function erp_api($method, $content, $sid, $app_key, $app_secret, $url)
{
    date_default_timezone_set('PRC');
    $params = array(
        'method' => $method,
        'timestamp' => date('Y-m-d H:i:s'),
        'format' => 'xml',
        'appkey' => $app_key,
        'sign_method' => 'md5',
        'sid' => $sid
    );

    $params['sign'] = MakeSign($app_secret, $params, $content, true);

    $opts = array(
        'http' => array(
            'method' => 'POST',
            'header' => "Content-Type: text/xml",
            'content' => $content
        )
    );
    $context = stream_context_create($opts);
    $service_url = $url . '?' . http_build_query($params);
    echo ($service_url . '\n');

    $response = file_get_contents($service_url, 0, $context);
    echo ($response);
    return $response;
}

//测试调用代码
$url = "http://39.105.14.222/wms_api/wdt_service.php";
$method = "WDT_WMS_DELIVERYORDER_CREATE";
$app_key = "以实际联调人员分配为准";
$app_secret = "以实际联调人员分配为准";
$sid = "以实际联调人员分配为准";
$content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request><deliveryOrder><deliveryOrderCode>postman_166260538551054698</deliveryOrderCode><orderType>JYCK</orderType><warehouseCode>warehouse_sly</warehouseCode><sourcePlatformCode>TM</sourcePlatformCode><sourcePlatformName></sourcePlatformName><logisticsCode>SFHH-XX</logisticsCode><createTime>2018-10-18 13:48:52</createTime><placeOrderTime>2018-10-18 13:47:25</placeOrderTime><payTime>2018-10-18 13:47:25</payTime><operateTime>2018-10-18 13:48:52</operateTime><shopNick>postman店铺</shopNick><buyerNick>买家</buyerNick><totalAmount>120</totalAmount><gotAmount>0</gotAmount><arAmount>100</arAmount><freight>0</freight><logisticsAreaCode>标准大头笔</logisticsAreaCode><invoiceFlag>N</invoiceFlag><buyerMessage/><serviceFee>110</serviceFee><sellerMessage>客服备注</sellerMessage><printRemark>打印备注</printRemark><senderInfo><name>1</name><mobile>1</mobile><province>天津市</province><city>天津市</city><area>滨海新区</area><detailAddress>华夏金融大厦</detailAddress></senderInfo><receiverInfo><name>倚天剑</name><idType>1</idType><idNumber>12344</idNumber><mobile> 13789888477 </mobile><tel></tel><province>河北省</province><city>邯郸市</city><area>邯山区</area><zipCode/><detailAddress>邯郸科技职业学院</detailAddress></receiverInfo><invoices><invoice><type>INVOICE</type><header>嘉兴河海中控信息</header><amount>868</amount><content>内容</content><taxNumber>我是税号</taxNumber></invoice></invoices><insurance><type>保险类型</type><amount>01</amount></insurance><deliveryRequirements><deliveryType>PTPS</deliveryType></deliveryRequirements><extendProps><flag_id>10</flag_id><wdtfx_platform>YZ</wdtfx_platform></extendProps></deliveryOrder><orderLines><orderLine><orderLineNo>1</orderLineNo><ownerCode>sly</ownerCode><itemCode>iphone</itemCode><itemId>409</itemId><inventoryType>ZP</inventoryType><itemName></itemName><sourceOrderCode>postman_166260538551054698</sourceOrderCode><subSourceOrderCode></subSourceOrderCode><payNo>payNo2</payNo><planQty>3</planQty><batchCode></batchCode><actualPrice>50</actualPrice><discountAmount>01</discountAmount><retailPrice>100</retailPrice><extendProps><erpDiscountRatio>0</erpDiscountRatio><fromMask>0</fromMask><suiteName>组合装</suiteName><suiteNum>1</suiteNum><payAccount>payAccount1</payAccount></extendProps><remark>备注</remark></orderLine></orderLines><extendProps><ConsolidationCode>集包地编码</ConsolidationCode><ConsolidationName>集包地名字，这个是可以打印的</ConsolidationName></extendProps></request>";
erp_api($method, $content, $sid, $app_key, $app_secret, $url);
